package com.eco.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/netty/KndEncoderCK.class */
public class KndEncoderCK extends MessageToByteEncoder<String> {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) KndEncoderCK.class);

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // io.netty.handler.codec.MessageToByteEncoder
    public void encode(ChannelHandlerContext channelHandlerContext, String msg, ByteBuf out) throws Exception {
        try {
            byte[] bytes = hexString2Bytes(msg);
            out.writeBytes(bytes);
        } catch (Exception e) {
            log.error("knd-tcp自定义编码器异常:{}", e.toString());
        }
    }

    private byte[] hexString2Bytes(String src) throws Exception {
        if (src.contains(StringUtils.SPACE)) {
            src = src.replaceAll(StringUtils.SPACE, "");
        }
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            ret[i] = Integer.valueOf(src.substring(i * 2, (i * 2) + 2), 16).byteValue();
        }
        return ret;
    }
}